local math = math
local filelog = require "filelog"
local base = require "base"
local qhbTool = {}

function qhbTool.gen_all_hbinfos(total_money, total_hb_nums, min_price, max_price)
	local all_hbinofs = {}
	local min_price = min_price or 1
	local max_price = max_price or math.floor(total_money * 3/total_hb_nums)

	for i = 1, total_hb_nums do
		local random_price = 0
		while true do
			if total_money < max_price then
				max_price = total_money
			end
			if total_hb_nums == i then
				random_price = total_money
				table.insert(all_hbinofs, { index = i, get_hb_money = random_price, hb_numbers = random_price % 10})
				break
			else
				random_price = base.get_random(min_price, max_price)
				if (math.floor((total_money - random_price)/(total_hb_nums - i)) - min_price > 0 ) then 
					table.insert(all_hbinofs, { index = i, get_hb_money = random_price, hb_numbers = random_price % 10})
					break
				end
			end
		end
		total_money = total_money - random_price
	end
	local real_all_num = 0
	for _, value in ipairs(all_hbinofs) do
		real_all_num = real_all_num + value.get_hb_money
	end
	return all_hbinofs
end

function qhbTool.gen_all_hbinfos_bylandnum(total_money, total_hb_nums, min_price, max_price, lei_nums, lei_hb_nums, lei_max_nums)
	
	local min_price = min_price or 1
	local max_price = max_price or math.floor(total_money * 2 / total_hb_nums)
	local lei_max_nums = lei_max_nums or 4

	local function gen_hbinfos(_temp_total_money, _temp_total_hb_nums, _temp_min_price, _temp_max_price)
		local hbinfos = {}
		local _temp_min_price = _temp_min_price or 1
		local _temp_max_price = _temp_max_price or math.floor(_temp_total_money * 3/_temp_total_hb_nums)

		for i = 1, _temp_total_hb_nums do
			local random_price = 0
			while true do
				if _temp_total_money < _temp_max_price then
					_temp_max_price = _temp_total_money
				end
				if _temp_total_hb_nums == i then
					random_price = _temp_total_money
					table.insert(hbinfos, { index = i, get_hb_money = random_price, hb_numbers = random_price % 10})
					break
				else
					random_price = base.get_random(_temp_min_price, _temp_max_price)
					if (math.floor((_temp_total_money - random_price)/(_temp_total_hb_nums - i)) - _temp_min_price > 0 ) then 
						table.insert(hbinfos, { index = i, get_hb_money = random_price, hb_numbers = random_price % 10})
						break
					end
				end
			end
			_temp_total_money = _temp_total_money - random_price
		end
		return hbinfos
	end
	local all_hbinofs = nil
	local real_all_num = 0
	local get_lei_hb_num = 0
	repeat
		real_all_num = 0
		get_lei_hb_num = 0
		all_hbinofs =  gen_hbinfos(total_money, total_hb_nums, min_price, max_price)
		for _, value in ipairs(all_hbinofs) do
			real_all_num = real_all_num + value.get_hb_money
			if lei_nums == value.hb_numbers then
				get_lei_hb_num = get_lei_hb_num + 1
			end
		end
	until( (real_all_num == total_money) and (get_lei_hb_num >= lei_hb_nums and get_lei_hb_num <= lei_max_nums))
	---filelog.sys_error("-----------all_hbinofs------------", all_hbinofs, lei_nums, lei_hb_nums, get_lei_hb_num)
	for i = 1, #all_hbinofs do
		local change_index = base.get_random(1, #all_hbinofs)
		all_hbinofs[i], all_hbinofs[change_index] = all_hbinofs[change_index], all_hbinofs[i]
	end
	---filelog.sys_error("----------luaxu------hou---------", all_hbinofs, lei_nums, lei_hb_nums, get_lei_hb_num)
	return all_hbinofs
end

return qhbTool